#include "path.h"
#include <QDebug>
Path * __Path = Q_NULLPTR;


QObject *Path_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
    Q_UNUSED(engine)
    Q_UNUSED(scriptEngine)

    // auto deleted
    __Path = new Path;

    return __Path;
}

Path::Path(QObject *parent) : QObject(parent)
{

}

QString Path::storage() const
{
    return QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QDir::separator() + qApp->organizationName();
}

QString Path::configRes() const
{
    QDir dir = this->storage();
    return dir.absoluteFilePath(QCoreApplication::applicationName() + ".fwnu");
}

QString Path::sdcard() const
{
    return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
}

QString Path::desktop() const
{
    return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
}

QString Path::pictures() const
{
    return QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
}

QString Path::movies() const
{
    return QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
}

QString Path::music() const
{
    return QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
}

QString Path::download() const
{
    return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
}

QString Path::camera() const
{
    QDir  dir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    bool ok = dir.cdUp();

    qDebug() << ok <<":" << dir.entryList();

   ok = dir.cd("DCIM");
   ok = dir.cd("Camera");

   qDebug() << dir.absolutePath();
    return dir.absolutePath();
}


QUrl Path::toUrl(const QString &fileName)
{
    return QUrl::fromLocalFile(fileName);
}

bool Path::mkpath(const QString &path)
{
    QDir dir = this->storage();

    return dir.mkpath(path);
}

QStringList Path::files(const QString &path)
{
    QDir dir(path);

    return dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
}

QString Path::toLocalFile(const QUrl &url)
{
    if (url.isLocalFile()) {
        return url.toLocalFile();
    }
    return url.toString();
}

bool Path::exists(const QString &fileName)
{
 return QFile::exists(fileName);
}


